package com.vitabuybuy.tv.ui.mine.talent.notice

import android.os.Bundle
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentNoticeBinding
import com.vitabuybuy.tv.dto.TabEntity
import com.vitabuybuy.tv.dto.TaskPoolDto
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.ui.mine.talent.notice.viewmodel.NoticeViewModel
import com.vitabuybuy.tv.utils.KeyBoardUtils
import kotlinx.android.synthetic.main.fragment_notice.*
import java.util.*

class NoticeFragment : BaseFragment<NoticeViewModel, FragmentNoticeBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_notice

    private val mTabEntities = ArrayList<CustomTabEntity>()

    private var pageNumber = 1
    private var status = ""
    private var params = HashMap<String, Any>()
    private var pageMap = HashMap<String, Any>().apply {
        this["size"] = 10
        this["desc"] = "id"
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding?.mViewModel = viewModel
        mTabEntities.apply {
            add(TabEntity(getString(R.string.all), 0, 0))
            add(TabEntity(getString(R.string.wait_to_deliver), 0, 0))
            add(TabEntity(getString(R.string.wait_to_receive), 0, 0))
            add(TabEntity(getString(R.string.wait_to_deal), 0, 0))
            add(TabEntity(getString(R.string.wait_auth), 0, 0))
            add(TabEntity(getString(R.string.no_pass), 0, 0))
            add(TabEntity(getString(R.string.already_pass), 0, 0))
        }
        mTabLayout_1.setTabData(mTabEntities)
    }

    override fun lazyLoadData() {
        searchCondition(pageNumber)
    }


    private fun searchCondition(pageNumber: Int) {
        KeyBoardUtils.hideSoftKeyboard(mContext)
        pageMap["page"] = pageNumber
        params["pageable"] = pageMap
        if (status.isNotEmpty()) {
            params["status"] = status
        } else {
            params.remove("status")
        }
        viewModel.getNoticePoolData(pageNumber, params)
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                searchCondition(pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                searchCondition(pageNumber)
            }
        })
        mTabLayout_1.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                status = if (position == 0) {
                    ""
                } else {
                    mTabEntities[position].tabTitle
                }
                pageNumber = 1
                searchCondition(pageNumber)
            }

            override fun onTabReselect(position: Int) {
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                searchCondition(pageNumber)
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                val item = msg.obj as TaskPoolDto
                if (item.type == "种草") {
                    PlantGrassNoticeDetailActivity.openActivity(mContext,item.id)
                } else {
                    LiveNoticeDetailActivity.openActivity(mContext,item.id)
                }
            }
        }
    }
}
